package com.mocha.bass.test.jiangan;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.hyzh.latte.NezhaApplication;
import com.hyzh.latte.vehicle.dmo.PointBO;
import com.hyzh.latte.vehicle.dmo.entity.PointPGEntity;
import com.hyzh.latte.vehicle.dmo.entity.PolePGEntity;
import com.hyzh.latte.vehicle.dmo.entity.PolePointPGEntity;
import com.hyzh.latte.vehicle.repository.PointPGRepository;
import com.hyzh.latte.vehicle.repository.PolePGRepository;
import com.hyzh.latte.vehicle.repository.PolePointPGRepository;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author songkui
 * @since 2024/3/29 23:36
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = NezhaApplication.class)
@Slf4j
public class PolePointUpdate {

    @Autowired
    private PolePointPGRepository polePointPGRepository;
    @Autowired
    private PointPGRepository pointPGRepository;

    @Test
    public void test(){
        List<PointPGEntity> pointList = pointPGRepository.lambdaQuery().eq(PointPGEntity::getParkCode, "jiangan").list();
        Map<String, PointPGEntity> pointMap = pointList.stream().collect(Collectors.toMap(PointPGEntity::getPointNo, Function.identity()));


        List<PolePointPGEntity> list = polePointPGRepository.lambdaQuery().eq(PolePointPGEntity::getParkCode, "jiangan").list();
        for (PolePointPGEntity entity : list){
            String json = pointJSON(pointMap, entity.getPoints());
            if (StrUtil.isEmpty(json)){
                log.error("PolePoint json IS NULL");
            }else {
                PolePointPGEntity ne = new PolePointPGEntity();
                ne.setId(entity.getId());
                ne.setPointJson(json);
                polePointPGRepository.updateById(ne);
            }
        }


    }


    private static String pointJSON(Map<String, PointPGEntity> pom, String points){
        String[] ss = points.split(",");
        List<PointBO> pointBOList = new ArrayList<>(ss.length);
        for (String s : ss){
            PointPGEntity entity = pom.get(s);
            if (null == entity){
                log.error("Point {} is null", s);
                return null;
            }

            PointBO pointBO = new PointBO();
            pointBO.setLon(entity.getLon());
            pointBO.setLat(entity.getLat());
            pointBO.setH(entity.getHeight());
            pointBO.setT(1);
            pointBO.setDistance(50);
            pointBOList.add(pointBO);
        }
        return JSON.toJSONString(pointBOList);
    }





}
